const fp = require('lodash/fp')

// const f = fp.flowRight(fp.replace(/\s+/g, '-'), fp.toLower)

// console.log(f('Hello   World'))

// world wide web ===> W. W. W
// 将字符串分割成数组，用split方法
// 将数组元素转为大写，并取每一项的首字母，用map遍历，用toUpper转大写，用first取首字母
// 数组生成字符串，join

// const firstLetterToUpper = fp.flowRight(fp.join('. '), fp.map(fp.flowRight(fp.first, fp.toUpper)),fp.split(' '))

// console.log(firstLetterToUpper('world wide web'))

const cars = [{
  name: 'Ferrari FF',
  horsepower: 660,
  dollar_value: 700000,
  in_stock: true
},
{
  name: 'Spyker C12 Zagato',
  horsepower: 650,
  dollar_value: 648000,
  in_stock: false
},
{
  name: 'Jaguar XKR-S',
  horsepower: 550,
  dollar_value: 132000,
  in_stock: false
},
{
  name: 'Audi R8',
  horsepower: 625,
  dollar_value: 114200,
  in_stock: false
},
{
  name: 'Aston Martin One-7',
  horsepower: 750,
  dollar_value: 1850000,
  in_stock: true
},
{
  name: 'Pagani Huayara',
  horsepower: 700,
  dollar_value: 1300000,
  in_stock: true
}
]

// function isLastInStock (cars) {
//   return fp.flowRight(fp.prop('in_stock'), fp.last)(cars)
// }

// // 2.打印测试
// console.log(isLastInStock(cars)) 

// function firstName(cars) {
//   return fp.flowRight(fp.prop('name'),fp.first)(cars)
// }

// // 2.打印测试
// console.log(firstName(cars))
// let _average = function (xs) {
//   return fp.reduce(fp.add, 0, xs) / xs.length
// }

// function averageDollarValue(cars) {
//   return fp.flowRight(_average,fp.map(fp.prop('dollar_value')))(cars)
// }

// // 2.打印测试
// console.log(averageDollarValue(cars))

let _underscore = fp.replace(/\W+/g, '_') // <--无须改动

// 1.实现 sanitizeNames 函数
function sanitizeNames(cars) {
  return fp.map(fp.flowRight(_underscore, fp.lowerCase, fp.prop('name')))(cars)
}

// 2.打印测试
// console.log(sanitizeNames(cars))
console.log(fp.flowRight(_underscore, fp.toLower, fp.prop('name'))(cars[0]))

// const i = cars.map(item => {
//   let name = item.name.toLowerCase()
//   name = _underscore(name)
//   return {...item, name}
// })

// console.log(i)

// console.log(fp.first(['sd', 'gf']))